<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>下载中心</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${ctxPath}/static/manage/css/x-admin.css" media="all">
    <link rel="stylesheet" href="${ctxPath}/static/manage/layui/css/layui.css" media="all">
    <script src="${ctxPath}/static/manage/layui/layui.js"></script>
    <script src="${ctxPath}/static/manage/js/jquery.js"></script>
</head>
<body>
    <div class="x-nav">
        <span class="nav-breadcrumb">
            <a><cite>后台管理></cite></a>
            <a><cite>下载中心></cite></a>
            <a><cite>文件列表</cite></a>
        </span>
    </div>
    <div class="x-body">
        <blockquote class="layui-elem-quote quoteBox">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input searchContent" placeholder="请输入文件名关键字" />
                </div>
                <a class="layui-btn search_btn" data-type="reload">搜索</a>
            </div>
            <div class="layui-btn" id="uploadFile">
                <i class="layui-icon">&#xe67c;</i>添加文件
            </div>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
        </blockquote>
        <table id="downloadTable" lay-filter="downloadTable"></table>
    </div>
</body>
</html>

<script type="text/javascript">
    layui.use(['jquery', 'upload', 'table', 'layer', 'util'], function(){
        var $ = layui.$, table = layui.table, layer = layui.layer, upload = layui.upload, util = layui.util;
        var active = {
            reload: function(){
                table.reload('search_table_id', {
                    where: {
                        content: $('.searchContent').val(),
                        page: 1
                    }
                });
            }
        };

        table.render({
            elem: '#downloadTable'
            ,id: 'search_table_id'
            ,height: 500
            ,url: 'download/downloadTable'
            ,page: true
            ,limit: 10
            ,limits : [10]
            ,cols: [[
                { checkbox: true, fixed: true },
                { field: 'id', title: '序号', width:'6%', sort: true, fixed: true },
                { field: 'fileName', title: '文件名称', width:'16%', sort: true },
                { field: 'articleTitle', title: '对应文章', width:'14%' },
                { field: 'uploadTime', title: '上传时间',width:'10%', sort: true, templet :function (d){return util.toDateString(d.uploadTime);}},
                { field: 'uploadUser', title: '上传人',width:'10%' },
                { field: 'state', title: '是否显示', width:'6%', event: 'state', templet: function(d){
                        var html = ['<input type="checkbox" name="state" lay-skin="switch" lay-text="是|否"'];
                        html.push(d.state == 1 ? ' checked' : '');
                        html.push('/>');
                        return html.join('');
                    }
                },
                { field: 'tempId', title: '操作', width:'15%', templet: function(d){
                        return '<a class="layui-btn layui-btn-xs" lay-event="edit" lay-id="'+ d.tempId +'"><i class="layui-icon layui-icon-edit"></i>排序</a>' +
                            '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" lay-id="'+ d.tempId +'"><i class="layui-icon layui-icon-delete"></i>移除</a>';
                    }
                }
            ]]
        });

        // 搜索（利用定义的active重新载入表格）
        $('.search_btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 添加文件 上传服务器
        upload.render({
            elem: '#uploadFile',
            size: 5000,
            exts: 'doc|docx|xls|xlsx|txt|pdf|zip|rar',
            url: '/manage/download/downloadAdd',
            done: function(result){
                layer.closeAll('loading');
                alert(result.data);
                window.location.reload();
            },
            error: function(result){
                layer.closeAll('loading');
                alert(result.data);
                window.location.reload();
            }
        });

        // 排序
        function editDownload() {
            alert("文件排序暂不能用（已默认按时间排序）");
        }

        // 监听工具条
        table.on('tool(downloadTable)', function (obj) {
            var data = obj.data, event = obj.event, tr = obj.tr;
            switch(event){
                case "edit":
                    editDownload(data);
                    break;
                case "state":
                    var stateValue = tr.find("input[name='state']").prop('checked') ? 1 : 2;
                    $.ajax({
                        type: 'post',
                        url: '/manage/download/updateDownloadState',
                        data: {'id': data.id, 'stateValue': stateValue},
                        success:function(result){
                            alert(result);
                            location.replace(location.href);                // 刷新
                        }
                    });
                    break;
                case "del":
                    layer.confirm('真的删除行么？', function(index){
                        layer.close(index);
                        $.ajax({
                            type: 'post',
                            url: '/manage/download/deleteDownload',
                            data: {'id': data.id},
                            success:function(result){
                                alert(result);
                                window.location.reload();      // 刷新
                            }
                        });
                    });
                    break;
            }
        });
    });

</script>
